package com.fzdy.dataplatform.controller;

import com.alibaba.fastjson.JSON;
import com.fzdy.dataplatform.controller.api.ModuleCallback;
import com.fzdy.dataplatform.controller.api.device.DeviceConfigApi;
import com.fzdy.dataplatform.core.common.AjaxCallback;
import com.fzdy.dataplatform.core.common.Check;
import com.fzdy.dataplatform.core.common.TimeStampTool;
import com.fzdy.dataplatform.core.controller.BaseController;
import com.fzdy.dataplatform.core.vo.Page;
import com.fzdy.dataplatform.core.vo.Seach;
import com.fzdy.dataplatform.entity.Device;
import com.fzdy.dataplatform.entity.DeviceConfig;
import com.fzdy.dataplatform.mongodb.DeviceData;
import com.fzdy.dataplatform.mongodb.DeviceDataDao;
import com.fzdy.dataplatform.service.DeviceConfigService;
import com.fzdy.dataplatform.service.DeviceService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping(value = "/sysmanage/device")
public class DeviceDateController extends BaseController {

    @Autowired
    private DeviceDataDao deviceDataDao;
    @Autowired
    private DeviceService deviceService;
    @Autowired
    private DeviceConfigService deviceConfigService;
    @RequestMapping(value = "/dataList")
    public String list(@RequestBody Page<DeviceData> page, HttpServletRequest request) {
        System.out.println(JSON.toJSONString(page));
        if (Check.checkStringNull(page.getName())){
            AjaxCallback ajaxCallback = new AjaxCallback(ModuleCallback.FAILURENO,"参数错误");
            return JSON.toJSONString(ajaxCallback);
        }
        if (Check.checkStringNull(page.getCreatedate())){
            page.setCreatedate(TimeStampTool.timeStampToString("yyyy-MM",new Timestamp(System.currentTimeMillis())));
        }
        deviceDataDao.findBySn(page.getName(),page);
        AjaxCallback ajaxCallback = new AjaxCallback(page.getList(), page.getTotalRow());
        System.out.println(JSON.toJSONString(page.getList()));
        return JSON.toJSONString(ajaxCallback);

    }

    @RequestMapping(value = "/configList")
    public String configList(@RequestBody Page<DeviceConfig> page, HttpServletRequest request) {
        if (Check.checkStringNull(page.getName())){
            AjaxCallback ajaxCallback = new AjaxCallback(ModuleCallback.FAILURENO,"参数错误");
            return JSON.toJSONString(ajaxCallback);
        }
        DeviceConfig deviceConfig=new DeviceConfig();
        deviceConfig.setSn(page.getName());
        if (page.getCreatedate()==null||"".equals(page.getCreatedate())){
            page.setCreatedate(TimeStampTool.timeStampToString("yyyy-MM",new Timestamp(System.currentTimeMillis())));
        }
        deviceConfigService.getPageByExampleEq(deviceConfig,page);
        AjaxCallback ajaxCallback = new AjaxCallback(dbToApi(page.getList()), page.getTotalRow());
        return JSON.toJSONString(ajaxCallback);

    }

    private List  dbToApi(List<DeviceConfig> list) {
        List<DeviceConfigApi> apis=new ArrayList<>();
        for (DeviceConfig deviceConfig:list){
            String createtime = TimeStampTool.DateToString("yy-MM-dd HH:mm:ss", deviceConfig.getCreatedate());

            DeviceConfigApi api=new DeviceConfigApi(deviceConfig.getId(),deviceConfig.getSn(),deviceConfig.getConfigid(),deviceConfig.getTimeservice(),deviceConfig.getConfigtype(),deviceConfig.getConfigdata(),createtime,deviceConfig.getStatus());
            apis.add(api);
        }
        return apis;
    }


    @RequestMapping(value = "/logList")
    public String logList(@RequestBody Page<DeviceData> page, HttpServletRequest request) {
        if (Check.checkStringNull(page.getName())){
            AjaxCallback ajaxCallback = new AjaxCallback(ModuleCallback.FAILURENO,"参数错误");
            return JSON.toJSONString(ajaxCallback);
        }
        if (page.getCreatedate()==null){
            page.setCreatedate(TimeStampTool.timeStampToString("yyyy-MM",new Timestamp(System.currentTimeMillis())));
        }
        deviceDataDao.findLogBySn(page.getName(),page);
        AjaxCallback ajaxCallback = new AjaxCallback(page.getList(), page.getTotalRow());
        return JSON.toJSONString(ajaxCallback);

    }

    @RequestMapping(value = "/getchartdata")
    public String  getchartdata (@RequestBody Seach seach, HttpServletRequest request) {
        System.out.println(JSON.toJSONString(seach));
        if (Check.checkStringNull(seach.getSn())){
            AjaxCallback ajaxCallback = new AjaxCallback(ModuleCallback.FAILURENO, "参数错误");
            return JSON.toJSONString(ajaxCallback);
        }
        Device device=deviceService.get(seach.getSn());
        if (Check.checkObjNull(device)){
            AjaxCallback ajaxCallback = new AjaxCallback(ModuleCallback.FAILURENO, "参数错误");
            return JSON.toJSONString(ajaxCallback);
        }
        if (!webUtilify.isAdmin(request)&&!device.getDeviceOwner().getUserid().equals(webUtilify.LoginUserinfo(request).getId())){
            AjaxCallback ajaxCallback = new AjaxCallback(ModuleCallback.FAILURENO, "参数错误");
            return JSON.toJSONString(ajaxCallback);
        }
        if (seach.getCreatedate()==null){
            seach.setCreatedate(TimeStampTool.timeStampToString("yyyy-MM",new Timestamp(System.currentTimeMillis())));
        }
        Map<String, Object> map = deviceDataDao.getchartdata(seach);
        AjaxCallback ajaxCallback = new AjaxCallback(ModuleCallback.SUCCESSNO, "请求成功");
        ajaxCallback.setData(map);
        return JSON.toJSONString(ajaxCallback);
    }





}
